16-2 ������O

MATLAB ¤ä´©¤GºØ±ø¥ó«ü¥O¡]Branching Command¡A©Î¤¤Ä¶¦¨¡u¤À¤ä«ü¥O¡v¡^¡A¤@ºØ¬O if-then-else ±ø¥ó«ü¥O¡A¥t¤@ºØ¬O MATLAB ¦b²Ä¤­ª©¤§«á¶}©l¤ä´©ªº switch - case - otherwise ±ø¥ó«ü¥O¡A¥H¤U¤À­z¤§¡C

³Ì±`¥Îªº±ø¥ó«ü¥O¬O if - then - else¡A¨ä¨Ï¥Î»yªk¬°¡G

if ±ø¥ó¦¡ ¹Bºâ¦¡¤@; else ¹Bºâ¦¡¤G; end

·í±ø¥ó¦¡¦¨¥ß®É¡AMATLAB ±N°õ¦æ¹Bºâ¦¡¤@¡A§_«h¡A´N°õ¦æ¹Bºâ¦¡¤G¡C­Y¤£»Ý¨Ï¥Î¹Bºâ¦¡¤G¡A«h¥iª½±µ¬Ù²¤ else ©M¹Bºâ¦¡¤G¡C¨Ò¦p¡G¦b¼Æ­È¹Bºâªº¹Lµ{¤¤¡A­YÅܼƭȬ° NaN¡]§Y Not A Number ¡^®É¡A§Ú­Ì­n¥ß¨è¦L¥Xĵ§i°T®§¡A¥i¿é¤J¦p¤U¡G

Example 1: 16-µ{¦¡¬yµ{±±¨î/if01.mx = 0/0; if isnan(x) disp('Warning: NaN detected!'); endWarning: NaN detected!

¦b¤W¨Ò¤¤¡Aisnan(x) ¥i¥Î©ó§PÂ_ x ¬O§_¬° NaN¡A­Y¬O¡A«h¶Ç¦^ 1¡]¯u¡^¡A§_«h§Y¶Ç¦^ 0¡]°°¡^¡C

¦b¤U¨Ò¤¤¡A§Ú­Ì¥i®Ú¾Ú¦V¶q y ªº¤¸¯À­È¬°©_¼Æ©Î°¸¼Æ¡A¨ÓÅã¥Ü¤£¦Pªº°T®§¡G

Example 2: 16-µ{¦¡¬yµ{±±¨î/if02.my = [0 3 4 1 6]; for i = 1:length(y) if rem(y(i), 2)==0 fprintf('y(%g) = %g is even.\n', i, y(i)); else fprintf('y(%g) = %g is odd.\n', i, y(i)); end endy(1) = 0 is even. y(2) = 3 is odd. y(3) = 4 is even. y(4) = 1 is odd. y(5) = 6 is even.

¤W­zªº if - then - else ¬°Âù¦V±ø¥ó¡A¥ç§Yµ{¦¡¥u·|°õ¦æ¡u¹Bºâ¦¡¤@¡v©Î¡u¹Bºâ¦¡ ¤G¡v¡A¤£·|¦³²Ä¤TºØ¥i¯à¡C

MATLAB ¥ç¥i°õ¦æ¦h¦V±ø¥ó¡A¨ä¨Ï¥Î»yªk¬°¡G

if ±ø¥ó¦¡¤@ ¹Bºâ¦¡¤@; elseif ±ø¥ó¦¡¤G ¹Bºâ¦¡¤G; elseif ±ø¥ó¦¡¤T ¹Bºâ¦¡¤T; else ¹Bºâ¦¡¥|; end

¥i¨£­Y­n¶i¦æ§ó¦h¦Vªº±ø¥ó¡A¥u»Ý¤@¦A­«ÂÐ elseif §Y¥i¡C¨Ò¦p¡A±ý§PÂ_ y ¦V¶q¤§¤¸¯À¬OÄÝ©ó 3n¡B3n¡Ï1¡B ©Î 3n+2¡A¥i¿é¤J¦p¤U¡G

Example 3: 16-µ{¦¡¬yµ{±±¨î/if03.my = [3 4 5 9 2]; for i = 1:length(y) if rem(y(i),3)==0 fprintf('y(%g)=%g is 3n.\n', i, y(i)); elseif rem(y(i), 3)==1 fprintf('y(%g)=%g is 3n+1.\n', i , y(i)); else fprintf('y(%g)=%g is 3n+2.\n', i , y(i)); end endy(1)=3 is 3n. y(2)=4 is 3n+1. y(3)=5 is 3n+2. y(4)=9 is 3n. y(5)=2 is 3n+2.

§Ú­Ì¤]¥i¥H¨Ï¥Î¯x°}©ó±ø¥ó¦¡¤¤¡A¦¹®É¥²»Ý¦b¯x°}¤¸¯À¬Ò¤£¬°¹s®É¡A±ø¥ó¦¡¤~ºâ¦¨¥ß¡C´«¥y¸Ü»¡¡G¡uif x, expression; end¡v§¹¥þµ¥®Ä©ó¡uif all(x(:)), expression; end¡v¡C¦ý¬O¥²»Ýª`·Nªº¬O¡G·í x ¬OªÅ¯x°}®É¡A±ø¥ó¦¡±N¥Ã»·¤£¦¨¥ß¡C

MATLAB ¤]¤ä´© switch-case-otherwise ªº¦h¦V±ø¥ó«ü¥O¡A¨ä¨Ï¥Î»yªk¦p¤U¡G

switch expression case value(1) statement(1) case value(2) statement(2) ... case value(n-1) statement(n-1) otherwise statement(n) end

¦b¤W­z»yªk¤¤¡Aexpression ¬°¤@¼Æ­È©Î¦r¦ê¡A·í¨ä­È©M value(k) ¬Ûµ¥®É¡AMATLAB §Y°õ¦æ statement(k) ¨Ã¸õ¥X switch «ü¥O¡C­Y expression ¤£µ¥©ó value(k)¡Ak=1, 2, ¡K, n-1¡A«h MATLAB ·|°õ¦æ statement(n) ¨Ã¸õ¥X switch «ü¥O¡C¨Ò¦p¡G±ý®Ú¾Ú¤ë¥÷¨Ó§PÂ_¨ä©u§O¡A¥i¿é¤J¦p¤U¡G

Example 4: 16-µ{¦¡¬yµ{±±¨î/switch01.mfor month = 1:12 switch month case {3,4,5} season = 'Spring'; case {6,7,8} season = 'Summer'; case {9,10,11} season = 'Autumn'; case {12,1,2} season = 'Winter'; end fprintf('Month %d ===> %s.\n', month, season); end Month 1 ===> Winter. Month 2 ===> Winter. Month 3 ===> Spring. Month 4 ===> Spring. Month 5 ===> Spring. Month 6 ===> Summer. Month 7 ===> Summer. Month 8 ===> Summer. Month 9 ===> Autumn. Month 10 ===> Autumn. Month 11 ===> Autumn. Month 12 ===> Winter.

¦b¤W¨Ò¤¤¡A§Ú­Ì¬Ù²¤¤F otherwise ±Ô­z¡C¦pªG expression ¬O¦r¦ê¡A¨º»ò­Y­n¦b case ¤§«á¤ñ¹ï¦h­Ó¦r¦ê¡A´N¥²»Ý¨Ï¥Î¦r¦êªº²§­È°}¦C¡]Cell Arrey of Strings¡^¡A¨Ò¦p¡G

Example 5: 16-µ{¦¡¬yµ{±±¨î/switch02.mmonth = {'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Dec', 'Nov', 'Dec'}; for i = 1:length(month) switch month{i} case {'Mar','Apr','May'} season = 'Spring'; case {'Jun','Jul','Aug'} season = 'Summer'; case {'Sep','Oct','Nov'} season = 'Autumn'; case {'Dec','Jan','Feb'} season = 'Winter'; end fprintf('%s is %s.\n', month{i}, season); endJan is Winter. Feb is Winter. Mar is Spring. Apr is Spring. May is Spring. Jun is Summer. Jul is Summer. Aug is Summer. Sep is Autumn. Dec is Winter. Nov is Autumn. Dec is Winter.

Hint
MATLAB ªº switch «ü¥O©M C »y¨¥ªº switch «ü¥O²¤¦³®t§O¡G¦b C »y¨¥ªº switch ±Ô­z¤º¡A¨C­Ó case ±Ô­z»Ý¥[¤W break ¥H¸õ¥X¸Ó switch ±Ô­z¡A¦Ó¦b MATLAB «h¤£¥²¦h¦¹¤@Á|¡C

¤@¯ë¦Ó¨¥¡Aswitch¡Vcase¡Votherwise ªº°õ¦æ®Ä²vÀu©ó if¡Vthen¡Velse ¡C¦]¦¹¡A§Ú­ÌÀ³¸Ó¦h¦h¨Ï¥Î switch¡Vcase¡Votherwise¡C


MATLABµ{¦¡³]­p¡G¤Jªù½g